<template>
  <div class="app-container">
    <div style="margin: 0 auto">
      <el-row :gutter="10">
        <el-col :span="12" :offset="6">
          <div style="text-align: center;margin-bottom: 30px;font-size: 18px">
            主机信息{{createStatus?'新增':'更新'}}
          </div>
          <el-form :model="hostForm" ref="hostForm" :rules="hostFormRules" label-width="100px">
            <el-form-item label="主机名称:" prop="name" required>
              <el-input type="text" v-model="hostForm.name" placeholder="请输入主机名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="企业:" prop="enterpriseId" >
              <el-select v-model="hostForm.enterpriseId" placeholder="请选择企业" style="width: 100%">
                <el-option v-for="(item,index) in enterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
              </el-select>
            </el-form-item>
<!--            <el-form-item label="主机id" prop="hostId">-->
<!--              <el-input type="text" v-model="hostForm.hostId" placeholder="" clearable></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="厂家:" prop="factory">
              <el-input type="text" v-model="hostForm.factory" placeholder="请输入厂家名称" clearable>
              </el-input>
            </el-form-item>
            <el-form-item label="设备数量:" prop="equNumber" >
              <el-input-number controls-position="right" type="text"  v-model="hostForm.equNumber" placeholder="请输入设备个数" clearable style="width: 100%">
<!--                <template slot="append">个</template>-->
              </el-input-number>
            </el-form-item>
          </el-form>
          <div style="text-align: center" class="dialog-footer">
            <el-button @click="cancelRegion">取 消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createRegion">创 建</el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updateRegion">修 改</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {addInHost, getInHostVo, updateInHost} from "@/api/host";

export default {
  name: "edit",
  computed:{
    createStatus(){
      return this.$route.query.ucId==undefined
    },
    // enterPriseHidden(){
    //   let roleCode = this.$store.getters.roleCode;
    //   return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
    // },
  },
  data(){
    return{
      hostForm:{

      },
      hostFormRules:{

      },

      addLoading:false,
      enterpriseList:[]
    }
  },
  created() {
    this.getInEnterpriseDominantList()
    if (!this.createStatus){
      this.getInHostVo()
    }
  },
  methods:{
    getInHostVo(){
      getInHostVo({ucId:this.$route.query.ucId}).then(res=>{
        this.hostForm = res.data
      }).catch(error=>{

      })
    },

    getInEnterpriseDominantList(){
      getInEnterpriseDominantList({deleted:false}).then(res=>{
        this.enterpriseList = res.data
      })
    },

    cancelRegion() {
      window.history.back()
    },
    createRegion(){
      this.addLoading = true
      this.$refs['hostForm'].validate(valid => {
        if (valid){
          addInHost().then(res=>{
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加主机信息成功"
            })
            this.cancelRegion()
          }).catch(res=>{
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        }else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateRegion(){
      this.addLoading = true
      this.$refs['hostForm'].validate(valid => {
        if (valid){
          updateInHost().then(res=>{
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新主机信息成功"
            })
            this.cancelRegion()
          }).catch(res=>{
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        }else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
  }
}
</script>

<style scoped>

</style>